package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Resource
    private ShopCartMapper shopCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetMealMapper setMealMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    //查看购物车
    @Override
    public List<ShoppingCart> SelectShopCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart>  shoppingCartList =shopCartMapper.selectShopCart(shoppingCart);

        return shoppingCartList;
    }
    //添加购物车
    @Override
    public void InsertShopCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //根据数据查询
        List<ShoppingCart> shoppingCartList=shopCartMapper.selectShopCart(shoppingCart);
       // log.info("=====判断链表是否为空======{} ===============",Objects.nonNull(shoppingCartList));
       // log.info("=====判断链表是否为空======{} ===============",Objects.nonNull(shoppingCartList)&&shoppingCartList.size()!=0);


        if (Objects.nonNull(shoppingCartList)&&shoppingCartList.size()!=0){
            log.info("============进入一级判断========");
            if (shoppingCartDTO.getDishId()!=null){
                log.info("============进入2级判断========");
                ShoppingCart shoppingCart1= shopCartMapper.selectByDishIdAnnFlovar(shoppingCartDTO);
                if (shoppingCart1!=null){
                   log.info("============进入3级判断========");
                    shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
                    log.error("=========得到的number{}====",shoppingCart1.getNumber()+1);
                    shopCartMapper.UpdateNumber(shoppingCart1);
                }else{
                    Dish dish = dishMapper.SelectedDishById(shoppingCartDTO.getDishId());
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setAmount(dish.getPrice());
                    shoppingCart.setImage(dish.getImage());
                    shopCartMapper.InsertShoppingCart(shoppingCart);
                }

            }else{
                ShoppingCart shoppingCart2= shopCartMapper.selectBySetmealId(shoppingCartDTO.getSetmealId());
                if (shoppingCart2!=null){
                    shoppingCart2.setNumber(shoppingCart2.getNumber()+1);
                    shopCartMapper.UpdateNumber(shoppingCart2);
                }else {
                    Setmeal setmeal =setMealMapper.selectBySetmealId(shoppingCartDTO.getSetmealId());
                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setAmount(setmeal.getPrice());
                    shoppingCart.setImage(setmeal.getImage());
                    shopCartMapper.InsertShoppingCart(shoppingCart);
                }
            }
        }else {
           // log.info("===================直接添加信息==============");
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            if (shoppingCartDTO.getDishId()!=null){
                Dish dish = dishMapper.SelectedDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shopCartMapper.InsertShoppingCart(shoppingCart);
            }else{
                Setmeal setmeal =setMealMapper.selectBySetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shopCartMapper.InsertShoppingCart(shoppingCart);
            }

        }
    }
    //通过useid清空购物车
    @Override
    public void CleanShoppingCart() {
        shopCartMapper.CleanShoppingCart(BaseContext.getCurrentId());
    }
    //删除购物车中一个商品
    @Override
    public void SubShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        if (shoppingCartDTO.getDishId()!=null){
            ShoppingCart shoppingCart = shopCartMapper.selectByDishIdAnnFlovar(shoppingCartDTO);
            if (shoppingCart.getNumber()>1){

                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shopCartMapper.UpdateNumber(shoppingCart);
            }else{
                //根据dishid删除本条数据
                shopCartMapper.deleteByDishId(shoppingCartDTO);
            }
        }else{
            ShoppingCart shoppingCart = shopCartMapper.selectBySetmealId(shoppingCartDTO.getSetmealId());
            if (shoppingCart.getNumber()>1){
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shopCartMapper.UpdateNumber(shoppingCart);
            }else{
                //根据SetmealId删除本条数据
                shopCartMapper.deleteBYSetmealId(shoppingCartDTO.getSetmealId());
            }
        }

    }
}
